package com.wuhongyu.controller;

import com.wuhongyu.entity.User;
import com.wuhongyu.service.UserService;
import com.wuhongyu.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class LoginController {
    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping("/toLogin")
    public String toLogin(User user, HttpSession session){
        User user1 = userService.login(user.getUsername(), user.getPassword());
        if(user1 == null){
            return "redirect:/login";
        }else {
            session.setAttribute("user",user1);
            return "redirect:/index";
        }
    }


    /**
     * 登出
     * @param session
     * @param response
     * @return
     * @throws IOException
     */
    @RequestMapping("/logout")
    public String logout(HttpSession session, HttpServletResponse response) throws IOException {
        session.invalidate();
        return "redirect:/login";
       //response.sendRedirect("/login");
    }

    @RequestMapping("/index")
    public String index(){
        return "index";
    }

    @RequestMapping("/login")
    public String login(){
        return "/login";
    }

    @RequestMapping("/footer")
    public String footer(){
        return "footer";
    }

    @RequestMapping("/index-ec")
    public String indexec(){
        return "index-ec";
    }

    @RequestMapping("/menu")
    public String menu(){
        return "menu";
    }

    @RequestMapping("/mywork")
    public String mywork(){
        return "mywork";
    }

    @RequestMapping("/top")
    public String top(){
        return "top";
    }

}